<template>
	<view class="productList">
		<view class="title">
			大衣哥工厂直营
		</view>
		<view class="btnBox">
			<view class="orderbtn" @click="handleOrder">
				订单列表
			</view>
			<view class="orderbtn" style="position: relative;">
				<button open-type="share" class="shareBtn"></button>
				转发
			</view>
		</view>

		<view class="dataList">
			<view class="dataItem" v-for="(item,index) in goodsList" @click="handleDetail(item.id)">
				<image class="" :src="item.images" style="width: 306rpx;height: 306rpx;" />
				<view class="contents">
					<view class="name">
						{{item.name}}
					</view>
					<view class="bottom">
						<view class="peice">
							￥{{item.price}}
						</view>
					</view>
					<view class="baodan">
						报单
					</view>
				</view>

			</view>
		</view>




		<!-- <u-popup :show="shareFlag" mode="bottom" :round="20" bgColor="transparent" @close="shareFlag = false">
			<view class="shareBox">
				<view class="title">
					立即分享
				</view>

				<view class="typeBox">
					<view class="typeitem">
						<image src="@/static/image/wx.png" mode="" style="width: 80rpx; height: 80rpx;"></image>
						<view class="">
							微信
							<button open-type="share" class="shareBtn"></button>
						</view>
					</view>
					<view class="typeitem">
						<image src="@/static/image/pyq.png" mode="" style="width: 80rpx; height: 80rpx;"></image>
						<view class="">
							朋友圈
						</view>
					</view>
				</view>

				<view class="btn" @click="shareFlag = false">
					取消
				</view>

			</view>
		</u-popup> -->


		<u-modal :show="show" :title="title" cancelText="计生专干" confirmText="村民" :showCancelButton="true"
			@cancel="handleYK" @confirm="handleCM"></u-modal>

	</view>
</template>

<script>
	import {
		goodsList,
		villagerLogin
	} from "@/api/api.js"
	export default {
		data() {
			return {
				goodsList: [],
				pageSize: 10,
				page: 1,
				share: {
					userId: "",
					pageId: "",
					logincode: "",
				},
				shareFlag: false,
				show: false,
				title: "您希望以什么身份进入该商城呢？",
			}
		},
		onShow() {
			wx.hideHomeButton();
		},
		onLoad(options) {
			// console.log("options",options)
			this.share.userId = options.share
			if (options.share) {
				this.show = true
			} else {
				this.handleLogin()
			}
			this.getgoodsList()
		},
		onShareAppMessage(e) {
			return {
				title: "大衣哥",
				path: `pages/home/priductList?share=${uni.getStorageSync("parentId")}`,
				imageUrl: "http://dayige-mini.oss-cn-beijing.aliyuncs.com/upload/images/202311/17/kmC7E5mobzDxCMBydahGs44GIPMO8wpViUlxjNUA.jpg"
				//此处传递share=true的目的是：可以在onload中接收到，从而判断是否是通过分享进来的。如果用不到，也可以不传。
			}
		},
		onShareTimeline(res) {
			if (res.from === 'button') { // 来自页面内分享朋友圈
				console.log(res.target)
			}
			return {
				title: '大衣哥',
				path: `pages/home/priductList?share=${uni.getStorageSync("parentId")}`,
				imageUrl: "http://dayige-mini.oss-cn-beijing.aliyuncs.com/upload/images/202311/17/kmC7E5mobzDxCMBydahGs44GIPMO8wpViUlxjNUA.jpg"
			}
		},
		methods: {
			handleShare() {
				uni.share({
					provider: "weixin",
					scene: "WXSceneTimeline",
					type: 1,
					summary: "https://www.pgyer.com/aZsMsq",
					success: function(res) {
						// console.log("success:" + JSON.stringify(res));
					},
					fail: function(err) {
						// console.log("fail:" + JSON.stringify(err));
					}
				})
			},
			handleLogin() {
				console.log("登录")
				let that = this
				uni.login({
					provider: 'weixin',
					success: async (loginRes) => {
						// console.log("微信获取code码", loginRes);
						that.logincode = loginRes.code
						let res = await villagerLogin({
							code: that.logincode,
							parent_id: that.share.userId
						})
						// console.log("村民登录", res)
						if (res.code == 1000) {
							uni.setStorageSync("token", res.data.token)
							uni.setStorageSync("userId", res.data.user_id)
							uni.setStorageSync("parentId", that.share.userId)
							// this.getgoodsList()
						}
					}
				})
			},

			async getgoodsList() {
				// let parentid = uni.getStorageSync("parentid")
				let res = await goodsList({
					page_size: this.pageSize,
					page: this.page,
					parent_id: this.share.userId
				})
				console.log("获取商品", res)
				if (res.code == 1000) {
					this.goodsList = res.data.data
					this.goodsList.forEach((item, index) => {
						item.images = item.image[0]
					})
				}
				// console.log("商品列表", res)
			},
			handleDetail(id) {
				this.share.pageId = id
				// console.log("this.share",JSON.stringify(this.share))
				// return
				uni.navigateTo({
					url: `./detail?share=${JSON.stringify(this.share)}`
				})
			},
			handleOrder() {
				uni.navigateTo({
					url: "./villagerOrder"
				})
			},

			handleYK() {
				this.show = false
				console.log("游客")
			},
			handleCM() {
				this.show = false
				console.log("村民")
				this.handleLogin()
			},
		}
	}
</script>

<style scoped lang="scss">
	.shareBox {
		width: 100%;
		height: 395rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 0rpx 0rpx;
		padding: 32rpx;
		box-sizing: border-box;

		.title {
			font-size: 36rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: 400;
			color: #333333;
			line-height: 48rpx;
			width: 100%;
			text-align: center;
		}

		.typeBox {
			width: 480rpx;
			margin: 26rpx auto;
			display: flex;
			justify-content: space-between;
			position: relative;

			.typeitem {
				width: 100rpx;
				text-align: center;
			}
		}

		.btn {
			width: 480rpx;
			height: 70rpx;
			background: #A2A2A2;
			border-radius: 512rpx 512rpx 512rpx 512rpx;
			font-size: 36rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 70rpx;
			text-align: center;
			margin: 0 auto;
			margin-top: 20rpx;
		}
	}

	.shareBtn {
		width: 326rpx;
		height: 100rpx !important;
		background-color: transparent;
		height: 80rpx;
		position: absolute;
		top: 0rpx;
		right: 0rpx;
		border: none !important;
		outline: none !important;
		// background-color: pink;
	}

	.shareBtn::after {
		border: none;
	}

	.btnBox {
		width: 100%;
		display: flex;
		justify-content: space-between;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.orderbtn {
		width: 340rpx;
		height: 88rpx;
		background-color: #FE5C5C;
		color: #FFFFFF;
		text-align: center;
		line-height: 88rpx;
		border-radius: 88rpx;
		font-weight: 700rpx;
		font-size: 35rpx;
		margin: 0 auto;
	}

	.title {
		font-size: 48rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 600;
		line-height: 48rpx;
		width: 100%;
		text-align: center;
		margin: 30rpx auto;
	}

	.productList {
		width: 100%;
		height: 100%;

		// background-color: ;
		.dataList {
			background: #F2F2F2;
			width: 100%;
			padding: 0 30rpx;
			box-sizing: border-box;
			padding-top: 20rpx;
			display: flex;
			flex-wrap: wrap;

			.dataItem {
				width: 326rpx;
				// height: 500rpx;
				background: #FFFFFF;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				margin-right: 35rpx;
				margin-bottom: 20rpx;
				box-shadow: 2px 2px 2rpx #F2F2F2;
				padding: 10rpx;
				box-sizing: border-box;

				.contents {
					padding: 0 20rpx;
					font-size: 48rpx;
					font-family: PingFang SC-Semibold, PingFang SC;
					font-weight: 600;
					color: #000000;
					line-height: 50rpx;

					.bottom {
						display: flex;
						justify-content: space-between;
						font-size: 48rpx;
						font-family: PingFang SC-Semibold, PingFang SC;
						font-weight: 600;
						color: #EC2929;
						line-height: 50rpx;
						margin-top: 10rpx;
					}

					.baodan {
						width: 112rpx;
						height: 62rpx;
						font-size: 32rpx;
						font-family: PingFang SC-Semibold, PingFang SC;
						font-weight: 600;
						color: #FFFFFF;
						background-color: #FE5C5C;
						text-align: center;
						line-height: 62rpx;
						border-radius: 10rpx;
						margin-top: 10rpx;
					}
				}
			}

			.dataItem:nth-child(2n) {
				margin-right: 0;
			}
		}

	}
</style>